package com.gillion.grule.client.core;

import com.gillion.grule.client.sync.DroolsRulePackage;
import com.gillion.grule.client.utils.Closure;
import com.gillion.grule.client.utils.Streams;
import java.util.HashMap;
import java.util.Map;







public class BusinessModel
{
    private Map<String, BusinessModelAttribute> attributes = new HashMap<>();







    public void init(final Context context, DroolsRulePackage droolsRulePackage) {
        Streams.of(droolsRulePackage.getBusinessModelAttributes())
                .forEach(new Closure<BusinessModelAttribute>()
                {
                    public void execute(BusinessModelAttribute businessModelAttribute) {
                        ExecuteComponent component = ComponentHolder.getComponent(businessModelAttribute.getValueType().name());
                        if (component != null) {
                            BusinessModelAttribute value;
                            if ("Constant".equals(businessModelAttribute.getValueType().name()) && !"Object".equals(businessModelAttribute.getDataType().name())) {
                                value = component.execute(businessModelAttribute.getDataType() + "|" + businessModelAttribute.getValueExpress(), context);
                            } else if ("InParameter".equals(businessModelAttribute.getValueType().name())) {
                                if (businessModelAttribute.getIsMust() != null) {
                                    value = component.execute(businessModelAttribute.getValueExpress() + "|" + businessModelAttribute.getIsMust() + "|" + businessModelAttribute.getName(), context);
                                } else {
                                    value = component.execute(businessModelAttribute.getValueExpress(), context);
                                }
                            } else {
                                value = component.execute(businessModelAttribute.getValueExpress(), context);
                            }
                            BusinessModel.this.attributes.put(businessModelAttribute.getName(), value);
                        }
                    }
                });
    }







    public <T> T attr(String attribute) {
        return (T)this.attributes.get(attribute);
    }

    public void attr(String attributeName, BusinessModelAttribute attributeValue) {
        this.attributes.put(attributeName, attributeValue);
    }


    public boolean startWith(String str, String code) {
        if (str.startsWith(code)) {
            return true;
        }
        return false;
    }


    public boolean endsWith(String str, String code) {
        if (str.endsWith(code)) {
            return true;
        }
        return false;
    }

    public boolean matchWith(String str, String code) {
        if (str.indexOf(code) > 0) {
            return true;
        }
        return false;
    }


    public Map<String, BusinessModelAttribute> getAttributes() {
        return this.attributes;
    }

    public void setAttributes(Map<String, BusinessModelAttribute> attributes) {
        this.attributes = attributes;
    }
}

